/*
 * Blog.java
 *
 * Created on March 7, 2009, 3:40 PM
 */

package gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.util.Vector;
import javax.swing.event.HyperlinkEvent;
import javax.swing.text.html.*;
import kernel.XMLPluginConfiguration;
import librss.*;
import kernel.net.OpenURL;
import org.jdom.Element;


/**
 *
 * @author  alvaro
 */
public class Blog extends javax.swing.JFrame implements ClipboardOwner {
    private Feed feed;
    private String s = System.getProperty("file.separator");
    private String blogDir;
    private String language_xml;
    /** Creates new form Blog */
    public Blog(Feed feed) throws Exception {
        initComponents();
        language_xml = Factory.getWorkspace() + s + "files" + s + "language.xml";
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((int) (dim.getWidth()/2)-(this.getWidth()/2), (int) (dim.getHeight()/2)-(this.getHeight()/2));
        this.blogDir = Factory.getWorkspace()+s+"blog";
        this.feed = feed;
        this.refactorCombobox();
        loadLanguage();
    }
    
    private void refactorCombobox() throws Exception{
        Vector<RSSItem> vec = feed.getAllItems();
        this.jComboBox1.removeAllItems();
        for(int i = 0;i<vec.size();i++){
            this.jComboBox1.addItem(vec.get(i).getTitle());
        }
    }

    private void loadLanguage(String language) {
        XMLPluginConfiguration xml = new XMLPluginConfiguration(language_xml);
        //menu texts
        jLabel2.setText(xml.getTag("/languages/" + language + "/windows/blog/date"));
        jLabel3.setText(xml.getTag("/languages/" + language + "/windows/blog/tags"));
        jMenuItem1.setText(xml.getTag("/languages/" + language + "/windows/blog/copy_url"));
    }

    public void loadLanguage() {
        XMLPluginConfiguration xml = new XMLPluginConfiguration(language_xml);
        java.util.List l = xml.selectNodes(xml.getDoc(), "/languages");
        String lang = ((Element) l.get(0)).getAttributeValue("selected");
        loadLanguage(lang);
    }
    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        fecha = new javax.swing.JTextField();
        labels = new javax.swing.JTextField();
        link = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();

        jMenuItem1.setText("Copiar direccion");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("RolGPS - Blog Reader");

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Fecha");

        jLabel3.setText("Etiquetas");

        jLabel4.setText("Link");

        fecha.setEditable(false);

        labels.setEditable(false);

        link.setEditable(false);
        link.setComponentPopupMenu(jPopupMenu1);
        link.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                linkMouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                linkMouseExited(evt);
            }
        });
        link.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                linkActionPerformed(evt);
            }
        });

        jEditorPane1.setContentType("text/html");
        jEditorPane1.setEditable(false);
        jEditorPane1.setEditorKit(new HTMLEditorKit());
        jEditorPane1.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
            public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
                jEditorPane1HyperlinkUpdate(evt);
            }
        });
        jScrollPane1.setViewportView(jEditorPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, 574, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(link, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
                            .addComponent(labels, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(labels, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void linkMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_linkMouseExited
// TODO add your handling code here:
        //this.link.setSelectionStart(0);
        //this.link.setSelectionEnd(0);
    }//GEN-LAST:event_linkMouseExited

    private void linkMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_linkMouseClicked
// TODO add your handling code here:
        //this.link.setSelectionStart(0);
        //this.link.setSelectionEnd(this.link.getText().length());
    }//GEN-LAST:event_linkMouseClicked

    private void linkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_linkActionPerformed
// TODO add your handling code here:
        
    }//GEN-LAST:event_linkActionPerformed
    
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        Vector<RSSItem> vec;
        try {
            vec = feed.getAllItems();
        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }
        for(int i = 0;i<vec.size();i++){
            if(vec.get(i).getTitle().equals(this.jComboBox1.getSelectedItem().toString())){
                this.link.setText(vec.get(i).getLink());
                String date = vec.get(i).getPubDate();
                if(date.contains("+")){
                    date = date.substring(0, date.lastIndexOf("+")-1);
                }
                this.fecha.setText(date);
                this.jEditorPane1.setText("<html><body>"+vec.get(i).getDescription()+"</body></html>");
                this.jEditorPane1.setCaretPosition(0);
                
                
                Vector<String> labels = vec.get(i).getTags();
                String sl = "";
                for(int j = 0;j<labels.size();j++){
                    if(j == labels.size()-1)
                        sl = sl + labels.get(j);
                    else
                        sl = sl + labels.get(j)+ ", ";
                }
                this.labels.setText(sl);
                break;
            }
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed
    
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
        
        StringSelection sl = new StringSelection(this.link.getText());
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sl, this);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jEditorPane1HyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_jEditorPane1HyperlinkUpdate
        // TODO add your handling code here:
        if(evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
            try {
                OpenURL.open(evt.getURL().toString());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_jEditorPane1HyperlinkUpdate
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new Blog().setVisible(true);
            }
        });
    }

    public void lostOwnership(Clipboard clipboard, Transferable contents) {
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField fecha;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField labels;
    private javax.swing.JTextField link;
    // End of variables declaration//GEN-END:variables
    
}
